package com.xcity.game.player.robot;

import java.util.ArrayList;
import java.util.List;

import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2Part;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;
import naga.x.util.NumberUtils;
import naga.x.util.StringUtils;

/**
 * 机器人
 * @author yang.li
 *
 */
@Excel(value = "robot.xlsx", sheet = 0)
public class RobotTemplate extends Template {

	/** 等级 */
	protected int level;
	
	/** 姓名 */
	protected String name;
	
	/** 头像 */
	protected String icon;

	/** 性别 */
	protected byte sex;

	/** 现金 */
	protected int gold;

	/** 钻石 */
	protected int stone;
	
	/** 最少可被偷现金 */
	protected int minBeStolenGold;
	
	/** 最多可被偷现金 */
	protected int maxBeStolenGold;
	
	/** 建筑信息 */
	protected String[] buildings;
	protected List<BuildingConfig> _buildings;
	
	public static class BuildingConfig {
		public int id;
		public int[] partsLvl;
		public int[] npcIds;
		public BuildingConfig(int id, int[] partsLvl, int[] npcIds) {
			this.id = id;
			this.partsLvl = partsLvl;
			this.npcIds = npcIds;
		}
		@Override
		public boolean equals(Object obj) {
			if (obj instanceof BuildingConfig) {
				return id == ((BuildingConfig) obj).id;
			}
			return false;
		}
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public byte getSex() {
		return sex;
	}

	public void setSex(byte sex) {
		this.sex = sex;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int getStone() {
		return stone;
	}

	public void setStone(int stone) {
		this.stone = stone;
	}

	public int getMinBeStolenGold() {
		return minBeStolenGold;
	}

	public void setMinBeStolenGold(int minBeStolenGold) {
		this.minBeStolenGold = minBeStolenGold;
	}

	public int getMaxBeStolenGold() {
		return maxBeStolenGold;
	}

	public void setMaxBeStolenGold(int maxBeStolenGold) {
		this.maxBeStolenGold = maxBeStolenGold;
	}

	public String[] getBuildings() {
		return buildings;
	}

	public void setBuildings(String[] buildings) {
		this.buildings = buildings;
	}
	
	public List<BuildingConfig> getWrappedBuildings() {
		return _buildings;
	}

	@Override
	public void validate() throws TemplateException {
		_buildings = new ArrayList<BuildingConfig>(buildings.length);
		for (String s : buildings) {
			// 建筑id:[部位1等级,部位2等级,部位3等级,部位4等级][槽位1npc,槽位2npc,槽位3npc,槽位4npc]
			
			// building id
			int n = s.indexOf(':');
			int id = NumberUtils.parseInt(s.substring(0, n).trim());
			// building parts level
			n = s.indexOf('[', n + 1);
			int n2 = s.indexOf(']', n + 1);
			String[] ss = StringUtils.split(s.substring(n + 1, n2), ",");
			int[] partsLvl = new int[BuildV2.PART_COUNT];
			for (int i = 0; i < partsLvl.length; i++) {
				n = NumberUtils.parseInt(ss[i]);
				partsLvl[i] = n >= 0 && n <= BuildV2Part.MAX_LEVEL ? n : 0;
			}
			// building's npc
			int[] npcIds = null;
			n = s.indexOf('[', n2 + 1);
			if (n != -1) {
				n2 = s.indexOf(']', n + 1);
				ss = StringUtils.split(s.substring(n + 1, n2), ",");
				npcIds = new int[BuildV2.MAX_SLOT];
				for (int i = 0; i < npcIds.length; i++) {
					npcIds[i] = NumberUtils.parseInt(StringUtils.trim(ss[i]));
				}
			}
			
			BuildingConfig config = new BuildingConfig(id, partsLvl, npcIds);
			if (_buildings.contains(config)) {
				throw new TemplateException("机器人建筑重复: id=" + id);
			}
			_buildings.add(config);
		}
		buildings = null;
	}
}
